#!/bin/bash

# Feng Admin 生产环境启动脚本 (Linux)
# 用于在生产环境中启动前后端服务

# 颜色定义
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

# 清屏
clear

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}  Feng Admin - 生产环境启动${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

# 检查项目根目录
if [ ! -d "apps/backend" ] || [ ! -d "apps/frontend" ]; then
    echo -e "${RED}错误: 请在项目根目录下运行此脚本${NC}"
    exit 1
fi

# Python 命令检测
if command -v python3 &> /dev/null; then
    PYTHON_CMD="python3"
elif command -v python &> /dev/null; then
    PYTHON_CMD="python"
else
    echo -e "${RED}✗ Python 未安装${NC}"
    exit 1
fi

# Node.js 检测
if ! command -v node &> /dev/null; then
    echo -e "${RED}✗ Node.js 未安装${NC}"
    exit 1
fi

# ==================== 环境检查 ====================

echo -e "${CYAN}[1/6] 检查环境配置...${NC}"

# 函数: 生产环境快速配置
prod_quick_setup() {
    echo ""
    echo -e "${BLUE}========================================${NC}"
    echo -e "${BLUE}  生产环境配置${NC}"
    echo -e "${BLUE}========================================${NC}"
    echo ""
    
    # 生成强密钥
    echo "正在生成安全密钥..."
    SECRET_KEY=$($PYTHON_CMD -c "import secrets; print(secrets.token_hex(32))" 2>/dev/null)
    JWT_SECRET_KEY=$($PYTHON_CMD -c "import secrets; print(secrets.token_hex(32))" 2>/dev/null)
    
    if [ -z "$SECRET_KEY" ]; then
        echo -e "${RED}错误: 无法生成密钥${NC}"
        exit 1
    fi
    
    echo -e "${GREEN}✓ 密钥已生成${NC}"
    echo ""
    
    # 域名配置
    echo -e "${YELLOW}服务器配置:${NC}"
    echo ""
    echo "请输入服务器域名或IP地址（如: yourwebsite.net 或 192.168.1.100）"
    read -p "域名/IP: " server_addr
    
    if [ -z "$server_addr" ]; then
        echo -e "${RED}错误: 域名/IP 不能为空${NC}"
        exit 1
    fi
    
    echo ""
    echo "请输入前端端口（如果80端口被占用，建议使用其他端口如8080）"
    read -p "前端端口 [8080]: " frontend_port
    frontend_port=${frontend_port:-8080}
    
    echo ""
    echo "请输入后端端口"
    read -p "后端端口 [5001]: " backend_port
    backend_port=${backend_port:-5001}
    
    echo ""
    echo "请选择协议:"
    echo "  1. HTTP"
    echo "  2. HTTPS (推荐)"
    read -p "请选择 (1-2) [2]: " protocol_choice
    if [ "$protocol_choice" = "1" ]; then
        protocol="http"
        echo -e "${YELLOW}⚠️  警告: HTTP 不安全，生产环境建议使用 HTTPS${NC}"
    else
        protocol="https"
    fi
    
    API_URL="${protocol}://${server_addr}:${backend_port}"
    FRONTEND_URL="${protocol}://${server_addr}:${frontend_port}"
    
    echo ""
    echo "是否需要配置数据库？（默认使用 SQLite）"
    echo "  1. SQLite (默认)"
    echo "  2. PostgreSQL"
    echo "  3. MySQL"
    read -p "请选择 (1-3) [1]: " db_choice
    
    case $db_choice in
        2)
            echo ""
            read -p "PostgreSQL 用户名: " db_user
            read -sp "PostgreSQL 密码: " db_pass
            echo ""
            read -p "数据库名称 [feng_admin]: " db_name
            db_name=${db_name:-feng_admin}
            read -p "数据库主机 [localhost]: " db_host
            db_host=${db_host:-localhost}
            DATABASE_URL="postgresql://${db_user}:${db_pass}@${db_host}/${db_name}"
            ;;
        3)
            echo ""
            read -p "MySQL 用户名: " db_user
            read -sp "MySQL 密码: " db_pass
            echo ""
            read -p "数据库名称 [feng_admin]: " db_name
            db_name=${db_name:-feng_admin}
            read -p "数据库主机 [localhost]: " db_host
            db_host=${db_host:-localhost}
            DATABASE_URL="mysql://${db_user}:${db_pass}@${db_host}/${db_name}"
            ;;
        *)
            # SQLite 使用 config.py 中的默认绝对路径，不在 .env 中设置
            DATABASE_URL=""
            ;;
    esac
    
    # 创建后端 .env
    cat > apps/backend/.env << EOF
# Feng Admin 后端生产环境配置
# 生成于 $(date)

FLASK_ENV=production
FLASK_DEBUG=False
SECRET_KEY=${SECRET_KEY}
JWT_SECRET_KEY=${JWT_SECRET_KEY}
JWT_ACCESS_TOKEN_EXPIRES=24
JWT_REFRESH_TOKEN_EXPIRES=30
CORS_ORIGINS=${FRONTEND_URL}
EOF
    
    # 只有非 SQLite 数据库才写入 DATABASE_URL
    if [ -n "$DATABASE_URL" ]; then
        echo "DATABASE_URL=${DATABASE_URL}" >> apps/backend/.env
    fi
    
    cat >> apps/backend/.env << EOF
GITHUB_TRENDING_CACHE_HOURS=4
LOG_LEVEL=WARNING
BACKEND_HOST=0.0.0.0
BACKEND_PORT=${backend_port}
EOF
    
    echo -e "${GREEN}✓ 后端配置已创建${NC}"
    
    # 创建前端 .env
    cat > apps/frontend/.env << EOF
# Feng Admin 前端生产环境配置
# 生成于 $(date)

VITE_API_BASE_URL=${API_URL}
VITE_APP_TITLE=Feng Admin
VITE_APP_VERSION=1.0.0
VITE_ENABLE_DEV_TOOLS=false
VITE_ENABLE_LOGGING=false
EOF
    
    echo -e "${GREEN}✓ 前端配置已创建${NC}"
    echo ""
    echo -e "${GREEN}========================================${NC}"
    echo -e "${GREEN}  配置完成！${NC}"
    echo -e "${GREEN}========================================${NC}"
    echo ""
    echo -e "${BLUE}配置信息:${NC}"
    echo -e "  服务器: ${CYAN}${server_addr}${NC}"
    echo -e "  前端: ${CYAN}${FRONTEND_URL}${NC}"
    echo -e "  后端: ${CYAN}${API_URL}${NC}"
    echo -e "  协议: ${CYAN}${protocol}${NC}"
    echo ""
    
    if [ "$protocol" = "http" ]; then
        echo -e "${RED}⚠️  安全提醒:${NC}"
        echo "  - 当前使用 HTTP，数据未加密"
        echo "  - 生产环境强烈建议配置 HTTPS"
        echo "  - 可以使用 Let's Encrypt 免费证书"
        echo ""
    fi
}

# 检查后端 .env
if [ ! -f "apps/backend/.env" ]; then
    echo -e "${RED}✗ 后端 .env 文件不存在${NC}"
    echo ""
    echo "请选择配置方式:"
    echo "  1. 生产环境快速配置 (推荐)"
    echo "  2. 完整配置向导"
    echo "  3. 手动配置后继续"
    echo ""
    read -p "请选择 (1-3) [1]: " config_choice
    config_choice=${config_choice:-1}
    
    case $config_choice in
        1)
            prod_quick_setup
            ;;
        2)
            if [ -f "scripts/setup-env.sh" ]; then
                chmod +x scripts/setup-env.sh
                ./scripts/setup-env.sh
            else
                echo -e "${YELLOW}完整向导不可用，使用快速配置...${NC}"
                prod_quick_setup
            fi
            ;;
        3)
            echo -e "${YELLOW}请手动创建 apps/backend/.env 文件后重新运行此脚本${NC}"
            exit 1
            ;;
        *)
            prod_quick_setup
            ;;
    esac
fi

# 检查前端 .env
if [ ! -f "apps/frontend/.env" ]; then
    echo -e "${YELLOW}⚠️  前端 .env 文件不存在，将使用默认配置${NC}"
fi

# 检查生产环境配置
if grep -q "FLASK_ENV=development" apps/backend/.env; then
    echo -e "${YELLOW}⚠️  警告: 后端配置为开发模式${NC}"
    read -p "是否继续? (y/N): " continue_dev
    if [[ ! $continue_dev =~ ^[Yy]$ ]]; then
        exit 1
    fi
fi

echo -e "${GREEN}✓ 环境配置检查完成${NC}"
echo ""

# ==================== 依赖检查 ====================

echo -e "${CYAN}[2/6] 检查依赖...${NC}"

# 检查 Python 虚拟环境
if [ ! -d ".venv" ]; then
    echo -e "${YELLOW}创建 Python 虚拟环境...${NC}"
    $PYTHON_CMD -m venv .venv
    if [ $? -ne 0 ]; then
        echo -e "${RED}✗ 虚拟环境创建失败${NC}"
        exit 1
    fi
fi

# 激活虚拟环境
source .venv/bin/activate

# 安装后端依赖
echo -e "检查后端依赖..."
pip install -q -r apps/backend/requirements.txt
if [ $? -ne 0 ]; then
    echo -e "${RED}✗ 后端依赖安装失败${NC}"
    exit 1
fi

# 安装前端依赖（构建需要 devDependencies）
echo -e "安装前端依赖..."
cd apps/frontend
npm ci  # 安装所有依赖（包括 devDependencies），构建需要
if [ $? -ne 0 ]; then
    echo -e "${RED}✗ 前端依赖安装失败${NC}"
    exit 1
fi
cd ../..

echo -e "${GREEN}✓ 依赖检查完成${NC}"
echo ""

# ==================== 构建前端 ====================

echo -e "${CYAN}[3/6] 构建前端...${NC}"

cd apps/frontend
npm run build
if [ $? -ne 0 ]; then
    echo -e "${RED}✗ 前端构建失败${NC}"
    exit 1
fi

# 构建完成后，清理 devDependencies 以节省空间
echo -e "清理开发依赖..."
npm prune --production
cd ../..

echo -e "${GREEN}✓ 前端构建完成${NC}"
echo ""

# ==================== 数据库初始化 ====================

echo -e "${CYAN}[4/6] 初始化数据库...${NC}"

# 确保 instance 目录存在
mkdir -p apps/backend/instance apps/backend/cache

cd apps/backend
$PYTHON_CMD -c "from app import app, db; app.app_context().push(); db.create_all(); print('数据库初始化完成')" 2>/dev/null || true
cd ../..

echo -e "${GREEN}✓ 数据库初始化完成${NC}"
echo ""

# ==================== 选择启动方式 ====================

echo -e "${CYAN}[5/6] 选择启动方式...${NC}"
echo ""
echo "  1. 使用 systemd 服务 (推荐)"
echo "  2. 使用 Supervisor"
echo "  3. 使用 PM2"
echo "  4. 使用 gunicorn + nginx (手动)"
echo "  5. 前台运行 (仅测试用)"
echo ""
read -p "请选择 (1-5): " start_mode

case $start_mode in
    1)
        echo ""
        echo -e "${BLUE}========================================${NC}"
        echo -e "${BLUE}  Systemd 服务配置${NC}"
        echo -e "${BLUE}========================================${NC}"
        echo ""
        echo "创建 systemd 服务文件:"
        echo ""
        echo "1. 后端服务 (/etc/systemd/system/feng-admin-backend.service):"
        echo ""
        cat << EOF
[Unit]
Description=Feng Admin Backend Service
After=network.target

[Service]
Type=simple
User=www-data
WorkingDirectory=/path/to/Feng_Admin
Environment="PATH=/path/to/Feng_Admin/.venv/bin"
ExecStart=/path/to/Feng_Admin/.venv/bin/gunicorn -w 4 -b 0.0.0.0:${backend_port} app:app
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
EOF
        echo ""
        echo "2. 启用并启动服务:"
        echo ""
        echo "  sudo systemctl daemon-reload"
        echo "  sudo systemctl enable feng-admin-backend"
        echo "  sudo systemctl start feng-admin-backend"
        echo "  sudo systemctl status feng-admin-backend"
        echo ""
        ;;
        
    2)
        echo ""
        echo -e "${BLUE}========================================${NC}"
        echo -e "${BLUE}  Supervisor 配置${NC}"
        echo -e "${BLUE}========================================${NC}"
        echo ""
        echo "创建 Supervisor 配置 (/etc/supervisor/conf.d/feng-admin.conf):"
        echo ""
        cat << EOF
[program:feng-admin-backend]
command=/path/to/Feng_Admin/.venv/bin/gunicorn -w 4 -b 0.0.0.0:${backend_port} app:app
directory=/path/to/Feng_Admin/apps/backend
user=www-data
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/feng-admin/backend.log
EOF
        echo ""
        echo "启动服务:"
        echo ""
        echo "  sudo supervisorctl reread"
        echo "  sudo supervisorctl update"
        echo "  sudo supervisorctl start feng-admin-backend"
        echo ""
        ;;
        
    3)
        echo ""
        echo -e "${BLUE}========================================${NC}"
        echo -e "${BLUE}  PM2 启动${NC}"
        echo -e "${BLUE}========================================${NC}"
        echo ""
        
        # 检查 PM2
        if ! command -v pm2 &> /dev/null; then
            echo "安装 PM2..."
            npm install -g pm2
        fi
        
        echo "使用 PM2 启动后端..."
        cd apps/backend
        pm2 start "gunicorn -w 4 -b 0.0.0.0:${backend_port} app:app" --name feng-admin-backend
        cd ../..
        
        echo ""
        echo "使用 PM2 服务前端 (如果需要)..."
        pm2 serve apps/frontend/dist ${frontend_port} --name feng-admin-frontend
        
        pm2 save
        pm2 startup
        
        echo ""
        echo -e "${GREEN}✓ PM2 启动完成${NC}"
        echo ""
        echo "常用命令:"
        echo "  pm2 list          - 查看进程列表"
        echo "  pm2 logs          - 查看日志"
        echo "  pm2 restart all   - 重启所有服务"
        echo "  pm2 stop all      - 停止所有服务"
        ;;
        
    4)
        echo ""
        echo -e "${BLUE}========================================${NC}"
        echo -e "${BLUE}  Gunicorn + Nginx 配置${NC}"
        echo -e "${BLUE}========================================${NC}"
        echo ""
        
        # 检查 gunicorn
        if ! .venv/bin/pip show gunicorn &> /dev/null; then
            echo "安装 gunicorn..."
            .venv/bin/pip install gunicorn
        fi
        
        echo "1. 启动 Gunicorn 后端:"
        echo ""
        echo "  cd apps/backend"
        echo "  ../../.venv/bin/gunicorn -w 4 -b 127.0.0.1:${backend_port} app:app --daemon"
        echo ""
        
        echo "2. Nginx 配置示例 (/etc/nginx/sites-available/feng-admin):"
        echo ""
        cat << EOF
# 后端 API 代理
server {
    listen 80;
    server_name api.yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:${backend_port};
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

# 前端静态文件
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    root /path/to/Feng_Admin/apps/frontend/dist;
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }
}
EOF
        echo ""
        echo "3. 启用 Nginx 配置:"
        echo ""
        echo "  sudo ln -s /etc/nginx/sites-available/feng-admin /etc/nginx/sites-enabled/"
        echo "  sudo nginx -t"
        echo "  sudo systemctl reload nginx"
        echo ""
        ;;
        
    5)
        echo ""
        echo -e "${YELLOW}⚠️  前台运行模式 (仅用于测试)${NC}"
        echo ""
        
        echo -e "${CYAN}[6/6] 启动服务...${NC}"
        
        # 启动后端
        echo "启动后端服务..."
        cd apps/backend
        ../../.venv/bin/python app.py &
        BACKEND_PID=$!
        cd ../..
        
        # 等待后端启动
        sleep 3
        
        # 前端已经构建，使用简单的 HTTP 服务器
        echo "启动前端服务..."
        cd apps/frontend/dist
        $PYTHON_CMD -m http.server ${frontend_port} &
        FRONTEND_PID=$!
        cd ../../..
        
        echo ""
        echo -e "${GREEN}========================================${NC}"
        echo -e "${GREEN}  服务已启动${NC}"
        echo -e "${GREEN}========================================${NC}"
        echo ""
        # 检查是否配置了域名
        if [ -f "apps/backend/.env" ] && grep -q "server_addr" apps/backend/.env 2>/dev/null; then
            # 从配置文件读取域名和协议
            eval $(grep "^CORS_ORIGINS=" apps/backend/.env)
            if [[ "$CORS_ORIGINS" == http://* ]] || [[ "$CORS_ORIGINS" == https://* ]]; then
                echo -e "前端: ${BLUE}${CORS_ORIGINS}${NC}"
                echo -e "后端: ${BLUE}${CORS_ORIGINS//:${frontend_port}/:${backend_port}}${NC}"
            else
                echo -e "前端: ${BLUE}http://localhost:${frontend_port}${NC}"
                echo -e "后端: ${BLUE}http://localhost:${backend_port}${NC}"
            fi
        else
            echo -e "前端: ${BLUE}http://localhost:${frontend_port}${NC}"
            echo -e "后端: ${BLUE}http://localhost:${backend_port}${NC}"
        fi
        echo ""
        echo -e "${YELLOW}按 Ctrl+C 停止服务${NC}"
        echo ""
        
        # 等待中断
        trap "echo ''; echo '正在停止服务...'; kill $BACKEND_PID $FRONTEND_PID 2>/dev/null; exit 0" INT
        wait
        ;;
        
    *)
        echo -e "${RED}无效的选项${NC}"
        exit 1
        ;;
esac

echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}  部署完成！${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo -e "${YELLOW}重要提示:${NC}"
echo "  1. 确保已配置防火墙规则"
echo "  2. 建议使用 HTTPS (配置 SSL 证书)"
echo "  3. 定期备份数据库文件"
echo "  4. 查看日志以监控运行状态"
echo ""
echo "查看文档: docs/DEPLOYMENT.md"
echo ""

